package com.fhminyi.GUI;

import com.fhminyi.dao.OrderItemDao;
import com.fhminyi.model.Product;
import com.fhminyi.model.User;
import com.fhminyi.dao.OrderDao;
import com.fhminyi.model.Order;
import com.fhminyi.model.OrderItem;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class PaymentFrame extends JFrame {
    private Product product;
    private User currentUser;
    private OrderDao orderDao = new OrderDao();
    private OrderItemDao orderItemDao = new OrderItemDao();

    public PaymentFrame(Product product, User currentUser) {
        this.product = product;
        this.currentUser = currentUser; // 初始化 currentUser
        setTitle("支付页面");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(5, 2));

        JLabel nameLabel = new JLabel("商品名称: " + product.getProductName());
        JLabel priceLabel = new JLabel("价格: " + product.getPrice());
        JLabel quantityLabel = new JLabel("数量: ");
        JSpinner quantitySpinner = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1)); // 默认数量为1，最小1，最大100，步长为1
        JLabel userLabel = new JLabel("用户名: " + currentUser.getUsername()); // 显示用户名
        JButton payButton = new JButton("支付");

        payButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        // 获取用户选择的数量
        int quantity = (int) quantitySpinner.getValue();

        // 创建订单并保存到数据库
        Order order = new Order();
        order.setUserId(currentUser.getUserId());
        order.setTotalPrice(product.getPrice() * quantity); // 根据数量计算总价
        order.setStatus("待处理");

        try {
            // 插入订单并获取生成的订单ID
            orderDao.addOrder(order);
            int orderId = order.getOrderId(); // 获取生成的订单ID

            if (orderId == 0) {
                throw new SQLException("订单插入失败，无法获取订单ID");
            }

            // 定义并初始化 orderItem 对象
            OrderItem orderItem = new OrderItem();
            orderItem.setOrderId(orderId);
            orderItem.setProductId(product.getProductId());
            orderItem.setQuantity(quantity); // 设置用户选择的数量
            orderItem.setPrice(product.getPrice());

            orderItemDao.addOrderItem(orderItem);

            JOptionPane.showMessageDialog(PaymentFrame.this, "支付成功: " + product.getProductName());
            dispose();
        } catch (SQLException ex) {
            // 如果支付失败，取消订单和订单项
            try {
                if (order.getOrderId() != 0) {
                    orderItemDao.deleteOrderItem(order.getOrderId());
                    orderDao.deleteOrder(order.getOrderId());
                    System.out.println("取消成功");
                }
            } catch (SQLException deleteEx) {
                JOptionPane.showMessageDialog(PaymentFrame.this, "取消订单失败: " + deleteEx.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
            JOptionPane.showMessageDialog(PaymentFrame.this, "支付失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            System.out.println("支付失败: " + ex.getMessage()); // 打印错误内容到控制台
        }
    }
});

        panel.add(nameLabel);
        panel.add(priceLabel);
        panel.add(quantityLabel);
        panel.add(quantitySpinner);
        panel.add(userLabel);
        panel.add(payButton);

        add(panel);
    }
}
